﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Runtime.CompilerServices;

using LRSTaxServices.DAL;
using LRSTaxServices.DAL.dsTaxDocumentsTableAdapters;
using LRSTaxServices.Common;

namespace LRSTaxServices.Biz
{
    /// <summary>
    /// TaxDocuments Business Object
    /// </summary>

    public class TaxDocuments : BOBase
    {
        #region Construction / Disposal

        /// <summary>
        /// Default constructor.
        /// </summary>
        protected TaxDocuments() { }

        #endregion

        #region Public Properties

        #region Instance Properties

        /// <summary>
        /// PKID of the TaxDocuments.
        /// </summary>
        public int DocumentID
        {
            get { return _documentid; }
            set
            {
                if (_documentid != value)
                {
                    if (OnPropertyChanging("DocumentID", _documentid, value))
                    {
                        int oldValue = _documentid;
                        _documentid = value;

                        OnPropertyChanged("DocumentID", oldValue, value);
                    }
                }
            }
        }  private int _documentid = -1;

        /// <summary>
        /// DocumentName .
        /// </summary>
        public string DocumentName
        {
            get { return _documentname; }
            set
            {
                if (_documentname != value)
                {
                    if (OnPropertyChanging("DocumentName", _documentname, value))
                    {
                        string oldValue = _documentname;
                        _documentname = value;
                        OnPropertyChanged("DocumentName", oldValue, value);
                    }
                }
            }
        }  private string _documentname = null;



        /// <summary>
        /// UploadedFileName .
        /// </summary>
        public string UploadedFileName
        {
            get { return _uploadedfilename; }
            set
            {
                if (_uploadedfilename != value)
                {
                    if (OnPropertyChanging("UploadedFileName", _uploadedfilename, value))
                    {
                        string oldValue = _uploadedfilename;
                        _uploadedfilename = value;
                        OnPropertyChanged("UploadedFileName", oldValue, value);
                    }
                }
            }
        }  private string _uploadedfilename = null;


        /// <summary>
        /// DocumentDescription.
        /// </summary>
        public string DocumentDescription
        {
            get { return _documentdescription; }
            set
            {
                if (_documentdescription != value)
                {
                    if (OnPropertyChanging("DocumentDescription", _documentdescription, value))
                    {
                        string oldValue = _documentdescription;
                        _documentdescription = value;
                        OnPropertyChanged("DocumentDescription", oldValue, value);
                    }
                }
            }
        }  private string _documentdescription = null;




        /// <summary>
        /// Date/time at which the Schedule was Updated.
        /// </summary>
        public DateTime? UpdatedDate
        {
            get { return _updateddate; }
            set
            {
                if (_updateddate != value)
                {
                    if (OnPropertyChanging("UpdatedDate", _updateddate, value))
                    {
                        DateTime? oldValue = _updateddate;
                        _updateddate = value;
                        OnPropertyChanged("UpdatedDate", oldValue, value);
                    }
                }
            }
        }  private DateTime? _updateddate = null;




        /// <summary>
        /// Date/time at which the Schedule was added.
        /// </summary>
        public DateTime? AddedDate
        {
            get { return _addedDate; }
            set
            {
                if (_addedDate != value)
                {
                    if (OnPropertyChanging("AddedDate", _addedDate, value))
                    {
                        DateTime? oldValue = _addedDate;
                        _addedDate = value;
                        OnPropertyChanged("AddedDate", oldValue, value);
                    }
                }
            }
        }  private DateTime? _addedDate = null;


        #endregion

        #endregion

        #region Public Methods

        #region CreateNew

        /// <summary>
        /// Creates a new, empty JobCategory object.
        /// </summary>
        public static TaxDocuments CreateNew()
        {
            TaxDocuments obj = new TaxDocuments();

            // Switch state to Added prior to initializing property values.
            obj.ObjState = ObjectState.Added;
            obj.AddedDate = DateTime.Now; ;
            return (obj);
        }


        [MethodImpl(MethodImplOptions.Synchronized)]
        public static TaxDocuments CreateNew(TaxDocumentsCollection collection)
        {
            TaxDocuments obj = CreateNew();

            // Get next/unique ID for the new object.
            int minVal = 0;
            foreach (TaxDocuments taxdocuments in collection)
            {
                if (taxdocuments.DocumentID < minVal)
                    minVal = taxdocuments.DocumentID;
            }
            obj.DocumentID = --minVal;
            collection.Add(obj);

            return (obj);
        }

        #endregion

        #region GetAllTaxDocuments

        /// <summary>
        /// Returns a collection of all TaxDocuments.
        /// </summary>
        public static TaxDocumentsCollection GetAllTaxDocuments()
        {
            TaxDocumentsTableAdapter adapter = new TaxDocumentsTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();

            dsTaxDocuments.TaxDocumentsDataTable tbl = adapter.GetData(null);

            TaxDocumentsCollection list = new TaxDocumentsCollection();

            if (tbl.Rows.Count > 0)
            {
                foreach (dsTaxDocuments.TaxDocumentsRow row in tbl.Rows)
                {
                    TaxDocuments TaxDocuments = new TaxDocuments();
                    RelationalToObject(TaxDocuments, row);
                    list.Add(TaxDocuments);
                }
            }

            return (list);
        }

        #endregion

        #region GetTaxDocumentsByID

        /// <summary>
        /// Returns a TaxDocuments object for the specified 'TaxDocumentsID' or null if no TaxDocuments with the specified parameter(s) exists.
        /// </summary>
        public static TaxDocuments GetTaxDocumentsByID(int documentID)
        {
            TaxDocumentsTableAdapter adapter = new TaxDocumentsTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();
            dsTaxDocuments.TaxDocumentsDataTable tbl = adapter.GetData(documentID);

            if (tbl.Rows.Count == 1)
            {
                TaxDocuments TaxDocuments = new TaxDocuments();
                RelationalToObject(TaxDocuments, tbl.Rows[0]);
                return (TaxDocuments);
            }
            else if (tbl.Rows.Count > 1)
                throw new ApplicationException("Database integrity error: Specified 'TaxDocuments' returned more than one TaxDocuments record.");

            return (null);
        }

        #endregion

        #region GetTaxDocumentsByUploadedFileName

        /// <summary>
        /// Returns a TaxDocuments object for the specified 'DocumentName' or null if no TaxDocuments with the specified parameter(s) exists.
        /// </summary>
        public static TaxDocuments GetTaxDocumentsByUploadedFileName(string uploadedfilename)
        {
            TaxDocumentsTableAdapter adapter = new TaxDocumentsTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();
            dsTaxDocuments.TaxDocumentsDataTable tbl = adapter.GetDataByUploadedFileName(uploadedfilename);

            if (tbl.Rows.Count == 1)
            {
                TaxDocuments TaxDocuments = new TaxDocuments();
                RelationalToObject(TaxDocuments, tbl.Rows[0]);
                return (TaxDocuments);
            }
            else if (tbl.Rows.Count > 1)
                throw new ApplicationException("Database integrity error: Specified 'TaxDocuments' returned more than one TaxDocuments record.");

            return (null);
        }

        #endregion

        #region Update

        /// <summary>
        /// Updates the specified TaxDocuments.
        /// </summary>
        public static void Update(TaxDocuments obj)
        {
            bool updateTaxDocuments = false;

            // Do not update invalid objects.
            if (!obj.Validate())
            {
                StringBuilder sb = new StringBuilder(2048);
                sb.Append("The object being updated has one or more invalid properties; the Update() operation for this object has been cancelled.\nError(s):");
                foreach (ValidationError err in obj.Errors)
                    sb.Append("\n* " + err.Element + ": " + err.Message);

                throw new InvalidObjectException(sb.ToString());
            }
            else
            {
                updateTaxDocuments = (obj.ObjState != ObjectState.Unchanged);

                if (!updateTaxDocuments)
                    return;
            }

            ObjectState originalObjectState = obj.ObjState;

            dsTaxDocuments.TaxDocumentsDataTable tbl = null;
            if (updateTaxDocuments)
            {
                TaxDocumentsTableAdapter adapter = new TaxDocumentsTableAdapter();
                adapter.Connection = AppSettings.GetDbConn();

                tbl = new dsTaxDocuments.TaxDocumentsDataTable();
                dsTaxDocuments.TaxDocumentsRow row = tbl.NewTaxDocumentsRow();
                ObjectToRelational(obj, tbl, row);

                adapter.Update(tbl);

                if (originalObjectState == ObjectState.Deleted)
                    obj = null;
                else RelationalToObject(obj, tbl.Rows[0]);
            }
        }

        #endregion

        #region Validate

        /// <summary>
        /// Validates the object based on its properties at the time of the call; updates the Errors property.
        /// </summary>
        public override bool Validate()
        {
            Errors.Clear();

            if (ObjState == ObjectState.Unchanged)
            {
                // By definition, unchanged objects are valid.
                return (true);
            }
            else if (ObjState == ObjectState.Uninitialized)
            {
                Errors.Add(new ValidationError("object", "Object is uninitialized.", ValidationError.Severity.Error));

                // No need to proceed past this error.
                return (false);
            }
            else if ((ObjState == ObjectState.Deleted) && (DocumentID <= 0))
            {
                // Deleted items must have 'real' (i.e. non-negative, non-zero) key(s) to be valid.
                Errors.Add(new ValidationError("DocumentID", "Deleted object has an invalid PKID.", ValidationError.Severity.Error));

                // No need to proceed past this error.
                return (false);
            }
            else  // ObjState == Added | Modified
            {

                // UserName is required.
                if ((DocumentName == null) || (DocumentName.Trim().Length == 0))
                {
                    Errors.Add(new ValidationError("FullName", "FullName is required.", ValidationError.Severity.Error));
                }
                else if (ObjState == ObjectState.Added)
                {

                }

            }

            return (Errors.Count == 0);
        }

        #endregion

        #region ToString

        /// <summary>
        /// User-friendly string represetation of this object.
        /// </summary>
        public override string ToString()
        {
            return (String.Format("TaxDocuments: DocumentID={0}, DocumentName={1}", DocumentID, DocumentName));
        }

        #endregion

        #endregion

    }

    /// <summary>
    /// Collection of 0 or more TaxDocuments objects.
    /// </summary>

    public class TaxDocumentsCollection : List<TaxDocuments>
    {
        #region Types

        public class UserComparer : IComparer<TaxDocuments>
        {
            public int Compare(TaxDocuments r1, TaxDocuments r2)
            {
                return (String.Compare(r1.DocumentName, r2.DocumentName));
            }
        }

        #endregion

        #region Public Methods

        #region FindByDocumentID

        public TaxDocuments FindByDocumentID(int documentid)
        {
            for (int ndx = 0; ndx < this.Count; ndx++)
                if (this[ndx].DocumentID == documentid)
                    return (this[ndx]);

            return (null);
        }

        #endregion

        #endregion
    }
}
